<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/2/13
 * Time: 10:55
 */

namespace app\admin\controller;


use think\Db;

class Expert extends Base
{
    /**
     * @return mixed|\think\response\Json
     * 达人分配记录
     */
    public function record(){
        if(request()->isAjax ()){
            $params = input('param.');
            $where = [];
            //开始时间
            if(isset($params['start_time']) && $params['start_time'] != '' && isset($params['end_time']) && $params['end_time'] == ''){
                $where['daren_time'] = ['>= time', $params['start_time']];
            }
            //结束时间
            if(isset($params['start_time']) && $params['start_time'] == '' && isset($params['end_time']) && $params['end_time'] != '')
            {
                $where['daren_time'] = ['<= time', $params['end_time']];
            }
            //开始&结束时间
            if(isset($params['start_time']) && $params['start_time'] != '' && isset($params['end_time']) && $params['end_time'] != '')
            {
                $where['daren_time'] = ['between time', [$params['start_time'], $params['end_time']]];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('daren_apply')->where($where)->count();// 获取总条数
            $lists = Db::name('daren_apply')
                ->join('up_super', 'daren_user_id = sup_id', 'left')
                ->join('up_shops', 'daren_shop_id = s_id', 'left')
                ->where($where)
                ->field('daren_id, daren_time, sup_name, s_name, daren_type, daren_money')
                ->page($Nowpage,$limits)
                ->order('daren_time desc')
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * @return \think\response\Json
     * 删除记录
     */
    public function darenDel(){
        $daren_id = input('id');
        $res = Db::name('daren_apply')->delete($daren_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}